/*
* Copyright (C) 2019 ~ 2021 Uniontech Software Technology Co.,Ltd.
*
* Author:     yanghongwei <yanghongwei@uniontech.com>
*
* Maintainer: yanghongwei <yanghongwei@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "tuogun_product_line.h"
#include "mysql_session.h"

#include <Poco/DateTimeFormatter.h>
#include <Poco/Data/Session.h>
#include <Poco/Exception.h>

#include <plog/Log.h>

using namespace Poco::Data;
using namespace Poco::Data::Keywords;

namespace Poco
{
    namespace Data
    {
        template<>
        class TypeHandler<struct TuogunProductLine>
        {
        public:
            static void bind(std::size_t pos, const TuogunProductLine &obj, AbstractBinder::Ptr pBinder, AbstractBinder::Direction dir) {
                // the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Age INTEGER(3))
                // Note that we advance pos by the number of columns the datatype uses! For string/int this is one.
                poco_assert_dbg (!pBinder.isNull());
                TypeHandler<uint32_t>::bind(pos++, obj.id, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.name, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.description, pBinder, dir);
                TypeHandler<int32_t>::bind(pos++, obj.pers_id, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.address, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.manager, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.link_man, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.link_mobile, pBinder, dir);
                TypeHandler<std::string>::bind(pos++, obj.link_address, pBinder, dir);
                TypeHandler<uint8_t>::bind(pos++, obj.is_enable, pBinder, dir);
                TypeHandler<Poco::DateTime>::bind(pos++, obj.create_time, pBinder, dir);
                TypeHandler<Poco::DateTime>::bind(pos++, obj.update_time, pBinder, dir);
            }
            static void extract(std::size_t pos, TuogunProductLine &obj, const TuogunProductLine &defVal, AbstractExtractor::Ptr pExt) {
                // defVal is the default person we should use if we encunter NULL entries, so we take the individual fields
                // as defaults. You can do more complex checking, ie return defVal if only one single entry of the fields is null etc...
                poco_assert_dbg (!pExt.isNull());
                // the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Age INTEGER(3))
                TypeHandler<uint32_t>::extract(pos++, obj.id, defVal.id, pExt);
                TypeHandler<std::string>::extract(pos++, obj.name, defVal.name, pExt);
                TypeHandler<std::string>::extract(pos++, obj.description, defVal.description, pExt);
                TypeHandler<int32_t>::extract(pos++, obj.pers_id, defVal.pers_id, pExt);
                TypeHandler<std::string>::extract(pos++, obj.address, defVal.address, pExt);
                TypeHandler<std::string>::extract(pos++, obj.manager, defVal.manager, pExt);
                TypeHandler<std::string>::extract(pos++, obj.link_man, defVal.link_man, pExt);
                TypeHandler<std::string>::extract(pos++, obj.link_mobile, defVal.link_mobile, pExt);
                TypeHandler<std::string>::extract(pos++, obj.link_address, defVal.link_address, pExt);
                TypeHandler<uint8_t>::extract(pos++, obj.is_enable, defVal.is_enable, pExt);
                TypeHandler<Poco::DateTime>::extract(pos++, obj.create_time, defVal.create_time, pExt);
                TypeHandler<Poco::DateTime>::extract(pos++, obj.update_time, defVal.update_time, pExt);
            }
            static void prepare(std::size_t pos, const TuogunProductLine &obj, AbstractPreparator::Ptr pPreparator) {
                // the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Age INTEGER(3))
                poco_assert_dbg (!pPreparator.isNull());
                TypeHandler<uint32_t>::prepare(pos++, obj.id, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.name, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.description, pPreparator);
                TypeHandler<int32_t>::prepare(pos++, obj.pers_id, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.address, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.manager, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.link_man, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.link_mobile, pPreparator);
                TypeHandler<std::string>::prepare(pos++, obj.link_address, pPreparator);
                TypeHandler<uint8_t>::prepare(pos++, obj.is_enable, pPreparator);
                TypeHandler<Poco::DateTime>::prepare(pos++, obj.create_time, pPreparator);
                TypeHandler<Poco::DateTime>::prepare(pos++, obj.update_time, pPreparator);
            }
            static std::size_t size() { return 12; }
        };
    }
}

void printf(const TuogunProductLine &device)
{
    LOGD << "TuogunProductLine[id:" << device.id << " "
            << "name:" << device.name << " "
            << "description:" << device.description << " "
            << "pers_id:" << device.pers_id << " "
            << "address:" << device.address << " "
            << "manager:" << device.manager << " "
            << "link_man:" << device.link_man << " "
            << "link_mobile:" << device.link_mobile << " "
            << "link_address:" << device.link_address << " "
            << "is_enable:" << (int32_t)device.is_enable << " "
            << "create_time:" << Poco::DateTimeFormatter::format(device.create_time, Poco::DateTimeFormat::ISO8601_FORMAT) << " "
            << "update_time:" << Poco::DateTimeFormatter::format(device.update_time, Poco::DateTimeFormat::ISO8601_FORMAT) << "]";
}

std::vector<TuogunProductLine> TuogunProductLineManager::getTuogunProductLineByPersId(int32_t persId)
{
    std::vector<TuogunProductLine> ver_device;
    try {
        if (MysqlSession::Instance()->session()) {
            Statement query(*MysqlSession::Instance()->session());
            query << "SELECT * FROM tuogun_product_line WHERE pers_id=? ORDER BY id",use(persId),into(ver_device),now;
        }
    } catch (Poco::Exception &e) {
        LOGW << "exception : " << e.what();
    }
    return ver_device;
}

TuogunProductLine TuogunProductLineManager::getTuogunProductLineByProductId(uint32_t productId)
{
    TuogunProductLine device;
    try {
        if (MysqlSession::Instance()->session()) {
            Statement query(*MysqlSession::Instance()->session());
            query << "SELECT * FROM tuogun_product_line WHERE id=?",use(productId),into(device),now;
        }
    } catch (Poco::Exception &e) {
        LOGW << "exception : " << e.what();
    }
    return device;
}

std::vector<TuogunProductLine> TuogunProductLineManager::getAllTuogunProductLine()
{
    std::vector<TuogunProductLine> ver_device;
    try {
        if (MysqlSession::Instance()->session()) {
            Statement query(*MysqlSession::Instance()->session());
            query << " SELECT * FROM tuogun_product_line",into(ver_device),now;

            for (auto device : ver_device)
                printf(device);
        }
    } catch (Poco::Exception &e) {
        LOGW << "exception : " << e.what();
    }
    return ver_device;
}
